package com.ce.common.db.dump.mysql;

import java.util.List;

import com.ce.common.db.base.DataObject;
import com.ce.common.db.base.Database;
import com.ce.common.db.dump.DumpCommand;

/**
 * MYSQL导出命令的实现
 * @author yangenxiong
 *
 */
public class MySQLDumpCommand implements DumpCommand {

	private Database database;
	
	public MySQLDumpCommand() {
		
	}
	
	public MySQLDumpCommand(Database database) {
		this.database = database;
	}
	
	public void setDatabase(Database database) {
		this.database = database;
	}
	
	public Database getDatabase() {
		return this.database;
	}
	
	/**
	 * 获取导出文件的命令
	 * @param tableNames 需要导出的表的对象集合
	 * @param outputFileName 导出的文件名(包括路径)
	 * @return 最后获取的命令
	 * @throws Exception
	 */
	public String getDumpCommand(List<DataObject> tableNames, 
			String outputFileName) throws Exception {
		StringBuffer buffer = new StringBuffer("cmd /c mysqldump -u" + database.getUser()
				+ " -p" + database.getPass() + " -h " + database.getHost()
				+ " " + database.getDatabaseName() + " ");
		if (tableNames != null && tableNames.size() != 0) {
			buffer.append("--tables ");
			for (int i = 0; i < tableNames.size(); i++) {
				buffer.append(tableNames.get(i).getName() + " ");
			}
		}
		if (database.getCharacterSet() != null && database.getCharacterSet() != "") {
			buffer.append("--default-character-set=");
			buffer.append(database.getCharacterSet() + " ");
		}
		buffer.append("> " + outputFileName);
		return buffer.toString();
	}

}
